import 'package:flutter/material.dart';
import 'package:hairen_app/server/server.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:fluttertoast/fluttertoast.dart';
class FravoritesAlert extends StatefulWidget{
  final int mid;
  final Function updatCallback;
  FravoritesAlert(this.mid,{this.updatCallback});
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new _FravoritesAlertState() ;
  }
}

class _FravoritesAlertState extends State<FravoritesAlert>{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return FutureBuilder(
        future: Server.getFavorite(),
        builder: (context,res){
        if (res.hasData) {
          List data=res.data['msg']['data'];
          return new SimpleDialog(
            title: data.length==0?new Text("暂无收藏夹"):new Text("选择收藏夹"),
            children: data.map((item){
              return new ListTile(
                  leading: Icon(
                    Icons.folder,
                    size: 50.0,
                    color: Theme.of(context).iconTheme.color,
                  ),
                  title: new Text("${item['name']}"),
                  subtitle: new Text("${item['count']}个视频"),
                  onTap: () {
                    Server.setFavorite(data: {"fid":"${item['id']}","mid":"${widget.mid}"}).then((ret){
                      if(ret['code']==1){
                        Fluttertoast.showToast(
                          msg: ret['msg'],
                          gravity: ToastGravity.CENTER,
                          toastLength: Toast.LENGTH_SHORT,
                          timeInSecForIos: 1,
                        );
                        Navigator.of(context).pop();
                        widget.updatCallback();
                      }else{
                        Fluttertoast.showToast(
                          msg: ret['msg'],
                          gravity: ToastGravity.CENTER,
                          toastLength: Toast.LENGTH_SHORT,
                          timeInSecForIos: 1,
                        );
                        Navigator.of(context).pop();
                      }
                    });
                  }
              );
            }).toList(),
          );
        }else{
         return new SpinKitWave(
            color: Color.fromRGBO(251, 114, 153, 1.0),
            size: 30.0,
          );
        }
        });
  }
}